package com.example.deployer;

import androidx.appcompat.app.AppCompatActivity;
import android.content.ComponentName;
import android.content.Context;
import android.content.Intent;
import android.content.ServiceConnection;
import android.os.Bundle;
import android.os.IBinder;
import android.os.RemoteException;
import android.util.Log;
import android.view.View;
import android.widget.Button;

import com.google.android.material.textfield.TextInputEditText;
import com.zijing2333.adaptivescheduling.IInteractionWithDeployer;

public class MainActivity extends AppCompatActivity {

    private static final String TAG = "fun_MainActivity_time";
    private IInteractionWithDeployer iInteractionWithDeployer = null;

    private ServiceConnection connection = new ServiceConnection() {
        @Override
        public void onServiceConnected(ComponentName componentName, IBinder iBinder) {//当服务连接后会回调用这个方法
            iInteractionWithDeployer = IInteractionWithDeployer.Stub.asInterface(iBinder);
        }

        @Override
        public void onServiceDisconnected(ComponentName componentName) {//当服务断开连接后会回调用这个方法
            iInteractionWithDeployer = null;
        }
    };

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        Button button = findViewById(R.id.btn_sendCommand);
        Button button1 = findViewById(R.id.btn_sendCommand1);
        Button button2 = findViewById(R.id.btn_sendCommand2);
        Button button3 = findViewById(R.id.btn_sendCommand3);

        Log.d(TAG, "onCreate: ===============0");

        //与切换服务的Service进行绑定
        Intent intent = new Intent();
        intent.setComponent(new ComponentName("com.zijing2333.adaptivescheduling","com.zijing2333.adaptivescheduling.InteractionWithDeployer.SwitcherInteractionWithDeployer"));
//        bindService(intent,connection, Context.BIND_AUTO_CREATE); //这里绑定了切换服务模块中的Service  说明那个Service与部署器模块的生命周期同步
        if(!bindService(intent,connection, Context.BIND_AUTO_CREATE)) {
            // bindService方法返回false，处理相应逻辑
            Log.d(TAG, "绑定失败");
        } else {
            // bindService方法返回true，绑定成功，等待onServiceConnected回调
            Log.d(TAG, "绑定成功");
        }
        button.setOnClickListener(this::sendCommand);
        button1.setOnClickListener(this::sendCommand1);

        button2.setOnClickListener(this::sendCommand2);
        button3.setOnClickListener(this::sendCommand3);
    }



    //A设备启动任务
    private void sendCommand(View view) {
        //sendTaskToSwitcher(in String taskId, in String taskCode, boolean isFrontendRequired, in String taskParams);

        String taskId = "002";
        String taskUrl = "https://gitee.com/and-xiaobai/zhangyi_he/raw/master/music6.html";
//        String taskUrl = "https://gitee.com/and-xiaobai/zhangyi_he/raw/master/gobang.html";
        boolean isFrontendRequired = true;
        int state =0;
        String sourceIP = "";
        int priority = 8;
        try {
            if (iInteractionWithDeployer == null){
                Log.d(TAG, "空指针异常");
            }else {
                iInteractionWithDeployer.sendTaskToSwitcher(taskId,taskUrl,isFrontendRequired,state,sourceIP,priority);
                Log.d(TAG, "部署器已发送通知给切换服务了");
            }
        } catch (RemoteException e) {
            throw new RuntimeException(e);
        }
    }


    //B设备上启动任务
    private void sendCommand1(View view) {
//        TextInputEditText et_ip_address = findViewById(R.id.et_ip_address);
//        String ip = et_ip_address.getText().toString();
        String ip = "192.168.137.51";
        String taskId = "002";
        String taskUrl = "https://gitee.com/and-xiaobai/zhangyi_he/raw/master/music6.html";
//        String taskUrl = "https://gitee.com/and-xiaobai/zhangyi_he/raw/master/gobang.html";
        boolean isFrontendRequired = true;
        int state = 1;
        String sourceIP = ip;
        int priority = 8;
        try {
            if (iInteractionWithDeployer == null){
                Log.d(TAG, "空指针异常");
            }else {
                long timestamp1 = System.currentTimeMillis();
                Log.d(TAG, "B设备部署器触通知切换器的时间:"+timestamp1);
                iInteractionWithDeployer.sendTaskToSwitcher(taskId,taskUrl,isFrontendRequired,state,sourceIP,priority);
                Log.d(TAG, "部署器已发送通知给切换服务了");
            }
        } catch (RemoteException e) {
            throw new RuntimeException(e);
        }
    }



    //测试方法、测试流程、测试用例、

    //A设备上启动任务----关键任务
    private void sendCommand2(View view) {
        String taskId = "001";
        String taskUrl = "https://gitee.com/and-xiaobai/zhangyi_he/raw/master/music6.html";
//        String taskUrl = "https://gitee.com/and-xiaobai/zhangyi_he/raw/master/test1.js";
        boolean isFrontendRequired = true;
        int state =0;
        String sourceIP = "";
        int priority = 11;
        try {
            if (iInteractionWithDeployer == null){
                Log.d(TAG, "空指针异常");
            }else {
                iInteractionWithDeployer.sendTaskToSwitcher(taskId,taskUrl,isFrontendRequired,state,sourceIP,priority);
                Log.d(TAG, "部署器已发送通知给切换服务了");
            }
        } catch (RemoteException e) {
            throw new RuntimeException(e);
        }
    }


    //B设备上启动任务----副本任务
    private void sendCommand3(View view) {
        TextInputEditText et_ip_address = findViewById(R.id.et_ip_address2);
        String ip = et_ip_address.getText().toString();
        String taskId = "001";
        String taskUrl = "https://gitee.com/and-xiaobai/zhangyi_he/raw/master/music6.html";
//        String taskUrl = "https://gitee.com/and-xiaobai/zhangyi_he/raw/master/test1.js";
        boolean isFrontendRequired = true;
        int state =1;
        String sourceIP = ip;
        int priority = 11;
        try {
            if (iInteractionWithDeployer == null){
                Log.d(TAG, "空指针异常");
            }else {
                iInteractionWithDeployer.sendTaskToSwitcher(taskId,taskUrl,isFrontendRequired,state,sourceIP,priority);
                Log.d(TAG, "部署器已发送通知给切换服务了");
            }
        } catch (RemoteException e) {
            throw new RuntimeException(e);
        }
    }





}